﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;
using System.Web;

namespace MyService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        private void BypassCrossDomain()
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
        }

        public List<DiaDiem> LayDiaDiem()
        {
            BypassCrossDomain();
            List<DiaDiem> diadiems = new List<DiaDiem>();
            //XElement elements = XElement.Load(HttpContext.Current.Server.MapPath("App_Data/DiaDiem.xml")).Descendants("DiaDiem");
            string path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/DiaDiem.xml");
            XDocument xdoc = XDocument.Load(path);
            var elements = xdoc.Descendants("DiaDiem");
            foreach (XElement e in elements)
            {
                DiaDiem dd = new DiaDiem();
                dd.Description = e.Attribute("description").Value;
                dd.Group = e.Attribute("type").Value;
                diadiems.Add(dd);
            }
            return diadiems;
        }


        public List<Item> LayItems()
        {
            BypassCrossDomain();
            List<Item> items = new List<Item>();
            string path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/DiaDiem.xml");
            XDocument xdoc = XDocument.Load(path);
            var elements = xdoc.Descendants("item"); 
                           
            foreach (XElement element in elements)
            {
                Item i = new Item();
                i.Id = element.Attribute("id").Value;
                i.Ten = element.Attribute("ten").Value;
                i.Group = element.Attribute("type").Value;
                i.TinhTrang = element.Attribute("tinhTrang").Value;
                i.Description = element.Attribute("description").Value;
                if (element.Attribute("latitude").Value != "")
                {
                    i.Latitude = double.Parse(element.Attribute("latitude").Value);
                }
                if (element.Attribute("longitude").Value != "")
                {
                    i.Longitude = double.Parse(element.Attribute("longitude").Value);
                }
                items.Add(i);
            }
            return items;
        }

        public List<Item> LayPublicItems()
        {
            //throw new NotImplementedException();
            BypassCrossDomain();
            List<Item> items = new List<Item>();
            string path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/DiaDiem.xml");
            XDocument xdoc = XDocument.Load(path);
            var elements = xdoc.Descendants("item");

            foreach (XElement element in elements)
            {
                if (element.Attribute("tinhTrang").Value == "public")
                {
                    Item i = new Item();
                    i.Id = element.Attribute("id").Value;
                    i.Ten = element.Attribute("ten").Value;
                    i.Group = element.Attribute("type").Value;
                    i.TinhTrang = element.Attribute("tinhTrang").Value;
                    i.Description = element.Attribute("description").Value;
                    if (element.Attribute("latitude").Value != "")
                    {
                        i.Latitude = double.Parse(element.Attribute("latitude").Value);
                    }
                    if (element.Attribute("longitude").Value != "")
                    {
                        i.Longitude = double.Parse(element.Attribute("longitude").Value);
                    }
                    items.Add(i);
                }
            }
            return items;
        }




        public string ThemItem(Item item)
        {
            BypassCrossDomain();
            string ketqua = "";
            try
            {
                string path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/DiaDiem.xml");
                XDocument xdoc = XDocument.Load(path);
                int max = xdoc.Root.Elements("DiaDiem").Elements("item").Max(l => int.Parse(l.Attribute("id").Value));
                item.Id = (max + 1).ToString();

                bool exist = false;
                var elements = xdoc.Descendants("item");
                foreach (XElement e in elements)
                {
                    if (e.Attribute("latitude").Value == item.Latitude.ToString() && e.Attribute("longitude").Value == item.Longitude.ToString())
                    {
                        exist = true;
                    }
                }

                if (exist)
                {
                    ketqua = "Đã Tồn Tại Vị Trí này";
                }
                else
                {
                    xdoc.Element("DanhSachDiaDiem")
                    .Elements("DiaDiem")
                    .Where(i => i.Attribute("type").Value == item.Group).FirstOrDefault()
                    .Elements("item")
                    .LastOrDefault().AddAfterSelf(
                    new XElement("item",
                    new XAttribute("id", item.Id),
                    new XAttribute("ten", item.Ten.ToString()),
                    new XAttribute("type", item.Group.ToString()),
                    new XAttribute("tinhTrang", item.TinhTrang.ToString()),
                    new XAttribute("description", item.Description.ToString()),
                    new XAttribute("longitude", item.Longitude.ToString()),
                    new XAttribute("latitude", item.Latitude.ToString())));
                    xdoc.Save(path);
                    ketqua = "Thêm Thành Công";
                }
                
            }
            catch (Exception ex)
            {
                string exception = ex.ToString();
                ketqua = "Thêm Thất Bại";
            }
            return ketqua;
        }

    }
}
